package com.hisense.ovcloud.mqtt.warehouse.dao;

import com.hisense.ovcloud.mqtt.warehouse.Constants;
import com.hisense.ovcloud.mqtt.warehouse.config.WarehouseExecutor;
import com.hisense.ovcloud.mqtt.warehouse.entity.device.WarehouseProductStatus;
import com.hisense.ovcloud.mqtt.warehouse.entity.warehouse.MqttDeviceWarehouse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WarehouseDao {

    public final WarehouseExecutor warehouseExecutor;

    public WarehouseDao(WarehouseExecutor warehouseExecutor) {
        this.warehouseExecutor = warehouseExecutor;
    }

    public WarehouseProductStatus getWarehouseProductStatus(String domain, String subDomain){
        return WarehouseProductStatus.model()
                .where(WarehouseProductStatus::getDomain).eq(domain)
                .where(WarehouseProductStatus::getSubDomain).eq(subDomain)
                .one();
    }

    public MqttDeviceWarehouse getByWifiId(String domain, String wifiId){
        String sql = Constants.Warehouse.SELECT_BY_WIFI_ID
                .replaceAll(Constants.Warehouse.DOMAIN_TO_REPLACE, domain)
                .replaceAll(Constants.Warehouse.WIFI_ID_TO_REPLACE, wifiId);
            return warehouseExecutor.directExecuteQueryOneAndMapToBean(sql, MqttDeviceWarehouse.class);
    }

    public boolean insertDeviceInfo(String domain, String subDomain, String wifiId, String publicKey, long code){
        String sql = Constants.Warehouse.INSERT_PREFIX
                .replaceAll(Constants.Warehouse.DOMAIN_ID_TO_REPLACE, domain)
                + String.format("('%s', '%s', %d, '%s', %d, %d, '%s', UTC_TIMESTAMP(), UTC_TIMESTAMP())",
                subDomain,
                wifiId,
                Constants.OldDays.MASTER,
                publicKey,
                Constants.OldDays.ACTIVE,
                code,
                ""
        ) + Constants.Warehouse.INSERT_SUFFIX;
        warehouseExecutor.directExecuteOneSQL(sql);
        return true;
    }

    public boolean registerProduct(String domain, String subDomain, String publicKey, String privateKey){
        WarehouseProductStatus.model()
                .setDomain(domain)
                .setSubDomain(subDomain)
                .setPublicKey(publicKey)
                .setPrivateKey(privateKey)
                .setStatus(Constants.OldDays.PRO_STATUS_ENCRYPT)
                .setType(Constants.OldDays.UNIFY)
                .setCreateTime("UTC_TIMESTAMP()")
                .saveOpt()
                .orElseThrow(RuntimeException::new);
        return true;
    }

    public boolean updateDeviceActivateStatus(String domain, String wifiId){
        String sql = Constants.Warehouse.UPDATE_STATUS
                .replaceAll(Constants.Warehouse.DOMAIN_ID_TO_REPLACE, domain)
                .replaceAll(Constants.Warehouse.STATUS_TO_REPLACE, String.valueOf(Constants.Warehouse.ACTIVATED))
                .replaceAll(Constants.Warehouse.WIFI_ID_TO_REPLACE, wifiId);
        warehouseExecutor.directExecuteOneSQL(sql);
        warehouseExecutor.directExecuteOneSQL(sql);
        return true;
    }
}
